package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "用户管理")
public class UserController {

    private final UserService userService;

    @PostMapping("/list")
    @ApiOperation(value = "用户列表查询",notes = "用户列表查询")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto")
    @ApiOperationSupport(includeParameters = {"userDto.realName"})
    public ResponseResult<List<UserVo>> list(UserDto userDto){
        log.info("用户列表查询，参数：{}",userDto);
        List<UserVo> userVoList = userService.list(userDto);
        return ResponseResult.success(userVoList);
    }

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页",notes = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"userDto.roleName"
                                             ,"userDto.dataState"
                                             ,"userDto.deptNo"
                                             ,"userDto.email"
                                             ,"userDto.roleId"
                                             ,"userDto.postNo"})
    public ResponseResult<PageResponse<UserVo>> Page(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        log.info("用户分页，参数：{},{},{}",userDto,pageNum,pageSize);
        PageResponse<UserVo> page = userService.page(userDto, pageNum, pageSize);
        return ResponseResult.success(page);
    }

    @PutMapping
    @ApiOperation(value = "用户添加",notes = "用户添加")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto")
    @ApiOperationSupport(includeParameters = {"userDto.realName"
                         ,"userDto.roleVoIds"
                         ,"userDto.deptNo"
                         ,"userDto.postNo"
                         ,"userDto.dataState"
                         ,"userDto.email"
                         ,"userDto.mobile"})
    public ResponseResult<Boolean> add(UserDto userDto){
        log.info("用户添加：{}",userDto);
        Boolean result = userService.add(userDto);
        return ResponseResult.success(result);
    }

    @PatchMapping
    @ApiOperation(value = "用户修改",notes = "用户修改")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto")
    @ApiOperationSupport(includeParameters = {"userDto.realName"
            ,"userDto.roleVoIds"
            ,"userDto.deptNo"
            ,"userDto.postNo"
            ,"userDto.dataState"
            ,"userDto.email"
            ,"userDto.mobile"})
    public ResponseResult<Boolean> update(UserDto userDto){
        log.info("用户修改：{}",userDto);
        Boolean result = userService.update(userDto);
        return ResponseResult.success(result);
    }

    @PutMapping ("/is-enable/{id}/{status}")
    @ApiOperation(value = "用户启用禁用", notes = "用户启用禁用")
    public ResponseResult updateStatus(@PathVariable Long id, @PathVariable String status) {
        log.info("用户启用禁用:{},{}", id, status);
        userService.updateStatus(id,status);
        return ResponseResult.success();
    }

    @DeleteMapping ("/remove/{userIds}")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    public ResponseResult delete(@PathVariable Long userIds) {
        log.info("删除用户:{}", userIds);
        userService.delete(userIds);
        return ResponseResult.success();
    }

    @PostMapping ("/reset-passwords/{userId}")
    @ApiOperation(value = "重置密码", notes = "重置密码")
    public ResponseResult<Boolean> resetPassword(@PathVariable Long userId) {
        Boolean result = userService.reset(userId);
        return ResponseResult.success(result);
    }
}


